package stuServlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.PreventRefresh;

import dao.StudentDAO;
import dto.StudentDTO;

@SuppressWarnings("serial")
public class doAddServlet extends HttpServlet {

	public doAddServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.process(request, response);

	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.process(request, response);

	}
	
	public void process(HttpServletRequest request,HttpServletResponse response){
		try {
			request.setCharacterEncoding("UTF-8");
		    String stuname=request.getParameter("stuName");
		    String stusex=request.getParameter("stuSex");
		    String year=request.getParameter("year");
		    String month=request.getParameter("month");
		    String day=request.getParameter("day");
		    String stubirth=year+"-"+month+"-"+day;
		    
		    StudentDTO dto=new StudentDTO(stuname,stusex,stubirth);
			if(PreventRefresh.chek(request)){
				  StudentDAO sdao=new StudentDAO();
			      sdao.add(dto);
			      request.getRequestDispatcher("doShowList").forward(request, response);
			}else{
				response.sendRedirect("stuJsp/error.jsp");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public void init() throws ServletException {
	}

}
